import { Terminal } from "xterm";
import "xterm/css/xterm.css";

export function useTerminal(options = {}) {
  const terminal = new Terminal(options);

  function loadAddon(addon) {
    terminal.loadAddon(addon);
  }

  function setOption(key, value) {
    terminal.setOption(key, value);
  }

  function open(container, socket) {
    // open termial
    terminal.open(container);
    // listening data from server
    socket.on("output", (data) => terminal.write(data));
    // send input via socket
    terminal.onData((data) => socket.emit("input", data));
  }

  return { loadAddon, setOption, open };
}
